/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mytdev.util.properties.formatters.impl;

import com.mytdev.util.properties.formatters.Formatter;
import java.awt.Font;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 *
 * @author Yann D'Isanto
 */
public class FontFormatter implements Formatter<Font> {

    /**
     * Font styles cache.
     */
    private static final Map<String, Integer> FONT_STYLES = new HashMap<String, Integer>();

    static {
        FONT_STYLES.put("bold", Font.BOLD);
        FONT_STYLES.put("italic", Font.ITALIC);
        FONT_STYLES.put("plain", Font.PLAIN);
    }

    /**
     * Parses a Font from the specified string then returns it.
     *
     * @param string the string to parse.
     * @return the parsed Font.
     * @throws ParseException if an error occurs.
     */
    public Font parse(String string) throws ParseException {
        String[] fontProperties = string.split(Pattern.quote(","));
        if (fontProperties.length != 3) {
            throw new ParseException("invalid font", 0);
        }
        String name = fontProperties[0].trim();
        if(name.length() == 0) {
            throw new ParseException("invalid font name", 0);
        }
        int style = Font.PLAIN;
        String styleProperty = fontProperties[1].trim();
        if (styleProperty.length() > 0) {
            String[] styles = styleProperty.split("\\s+");
            for (String fontStyle : styles) {
                style |= FONT_STYLES.get(fontStyle);
            }
        }
        int size = 0;
        try {
            size = Integer.parseInt(fontProperties[2].trim());
        } catch(NumberFormatException ex) {
            throw new ParseException("invalid font size", string.lastIndexOf(","));
        }
        return new Font(name, style, size);
    }

    /**
     * Formats the specified Font as a string representation.
     *
     * @param font the Font to format
     * @return the Font string representation or null if the specified Font is
     * null.
     */
    public String format(Font font) {
        if (font == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(font.getFamily()).append(",");
        if (font.isBold()) {
            sb.append(" bold");
        }
        if (font.isItalic()) {
            sb.append(" italic");
        }
        if (font.isPlain()) {
            sb.append(" plain");
        }
        sb.append(", ");
        return sb.append(font.getSize()).toString();
    }
}
